let rec power x n =
    match n with
        0 -> 1
        |n when n < 0 -> invalid_arg "n negatif"
        |_ -> x * power x (n-1);;



let rec application polynome x =
     match polynome with
         [] -> 0
         |(c,p)::l -> c * power x p + application l x;;

application [(2,2);(2,3);(4,4)]2;;



let rec add  po1 po2 = 
    match (po1,po2) with
        ([],[]) -> []
        |(po1,[]) -> po1
        |([],po2) -> po2
        |((c1,p1)::l1 , (c2,p2)::l2) -> 
            if p1 = p2 then 
            (c1 + c2 , p1) :: add l1 l2
            else
                if p1 < p2 then 
                (c1,p1) :: add l1 po2
                else
   	        (c2,p2) :: add po1 l2;; 

add[(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;



let rec soustract  po1 po2 = 
     match (po1,po2) with
         ([],[]) -> []
         |(po1,[]) -> po1
         |([],po2) -> po2
         |((c1,p1)::l1 , (c2,p2)::l2) -> 
             if p1 = p2 then  
             (c1 - c2 , p1) :: soustract l1 l2
             else
                  if p1 < p2 then (c1,p1) :: soustract l1 po2
        	  else
	          (-c2,p2) :: soustract po1 l2;; 


soustract [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;



let rec deriv po1 =
    match po1 with
        [] -> []
        |(c,p)::l -> ((p * c),(p-1)):: deriv l;;

deriv [(2,2);(2,3);(4,4)];;

